using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Serialization;
using System.Security.Policy;
using System.Reflection;

namespace JoinMe
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                IPHostEntry entry = Dns.GetHostEntry("localhost");
                IPEndPoint srcPoint = new IPEndPoint(entry.AddressList[0], 20001);

                IPEndPoint destPointIsolated = IsolatedStorage.GetIPEndPoint();

                Socket so = new Socket(srcPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    so.Connect(destPointIsolated);
                    byte[] data = Encoding.UTF8.GetBytes(this.textBox1.Text.PadRight(140));
                    so.Send(data);
                    so.Close();
                    this.label1.Text = "Your message was sent !";
                    this.textBox1.Text = "";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    this.label1.Text = "Your message was NOT sent";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                IPHostEntry entry = Dns.GetHostEntry("localhost");
                IPEndPoint srcPoint = new IPEndPoint(entry.AddressList[0], 20001);

                IPEndPoint destPointIsolated = null;
                if (IsolatedStorage.VerifyStore())
                {
                    destPointIsolated = IsolatedStorage.GetIPEndPoint();
                }
                else
                {
                    Password p = new Password();
                    DialogResult dr = p.ShowDialog(this);
                    if (dr == DialogResult.OK)
                    {
                        destPointIsolated = IsolatedStorage.GetIPEndPoint();
                    }
                    else
                    {
                        this.Close();
                        return;
                    }
                }
                Socket so = new Socket(srcPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    so.Connect(destPointIsolated);
                    so.Close();
                    this.groupBox1.Text = "Write a message";
                    this.pictureBox1.Image = this.pictureBox1.InitialImage;
                    this.textBox1.Text = "";
                    this.textBox1.Enabled = true;
                    if (String.IsNullOrEmpty(this.textBox1.Text))
                    {
                        this.button1.Enabled = false;
                    }
                    else
                    {
                        this.button1.Enabled = true;
                    }
                }
                catch (Exception)
                {
                    this.groupBox1.Text = "My host is not reachable";
                    this.textBox1.Text = "WARNING : Check your windows firewall";
                    this.textBox1.Enabled = false;
                    this.button1.Enabled = false;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                throw;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.textBox1.Text))
            {
                this.button1.Enabled = false;
            }
            else
            {
                this.button1.Enabled = true;
            }
        }
    }
}